// 引入mockjs
import Mock from "mockjs"; //用户管理

let tableUserList = Mock.mock({
  "list|45": [
    {
      key: "@increment(1)",
      userName: "@name",
      nickName: "@cname",
      deptName: '@pick(["研发部门", "管理部门", "人事部", "行政部门"])',
      phonenumber: /^1[3-9]\d{9}$/, // 随机生成11位手机号（纯数字）  @string("number", 11)
      status: "@integer(0,1)", // 随机生成 0 或 1
      createTime: '@datetime("yyyy-MM-dd HH:mm:ss")', // 随机生成时间（年月日 时分秒）
      userId: "@increment(1)",
      cardNo: "@integer(100000000000, 999999999999)", //卡号
    },
  ],
}).list;

//角色管理
let tableRoleList = Mock.mock({
  "list|45": [
    {
      key: "@increment(1)",
      roleName: '@pick(["超级管理员", "管理员", "本部门以下", "自定义角色"])',
      authority: '@pick(["admin", "zby", "supper", "menu"])',
      showIndex: "@integer(1, 20)",
      status: "@integer(0,1)", // 随机生成 0 或 1
      createTime: "@integer(100000000000, 999999999999)", // 随机生成时间（年月日 时分秒）
      timestamp: "@timestamp",
      userId: "@increment(1)",
    },
  ],
}).list;

// 获取用户列表
Mock.mock("/api/userList", "post", function (option) {
  const { body } = option;
  let list = tableUserList;
  if (body) {
    const obj = JSON.parse(body);
    const { username, phone, status, cardNo } = obj;

    list = tableUserList.filter((item) => {
      // 如果 name 存在且非空，才检查 item.nickName.includes(name)
      const nameMatch = username ? item.nickName.includes(username) : true;
      // console.log('nameMatch', nameMatch);
      // 如果 phone 存在且非空，才检查 item.phonenumber.includes(phone)
      const phoneMatch = phone ? item.phonenumber.includes(phone) : true;
      const statusMatch = status ? Number(status) === item.status : true;
      // 卡号搜索
      const cardMatch = cardNo ? Number(cardNo) === item.cardNo : true;

      // 只有 nameMatch 和 phoneMatch 都为 true，才保留这个项
      return nameMatch && phoneMatch && statusMatch && cardMatch;
    });

    console.log("list", list, body);
  }
  return {
    code: "0",
    msg: "success",
    tableList: list,
  };
});

// 编辑单个用户信息
Mock.mock("/api/upDateUserList", "post", function (option) {
  const { body } = option;
  let list = tableUserList;
  if (body) {
    const obj = JSON.parse(body);
    const { userId } = obj;

    list = tableUserList.map((item) => {
      return item.userId === userId ? { ...item, ...obj } : item;
    });

    console.log("list", list, body);
  }
  return {
    code: "0",
    msg: "success",
    tableList: list,
  };
});
//删除用户
Mock.mock("/api/delIds", "post", function (option) {
  const { body } = option;
  // let list = tableUserList
  if (body) {
    const obj = JSON.parse(body);
    const ids = obj.ids;
    console.log("obj", ids);
    tableUserList = tableUserList.filter((item) => !ids.includes(item.key));
  }
  return {
    code: "0",
    msg: "success",
    tableList: tableUserList,
  };
});

// 获取角色列表
Mock.mock("/api/roleList", "post", function (option) {
  // console.log('option',option);
  const { body } = option;
  let list = tableUserList;
  if (body) {
    const obj = JSON.parse(body);
    const { username, phone, status } = obj;

    list = tableUserList.filter((item) => {
      // 如果 name 存在且非空，才检查 item.nickName.includes(name)
      const nameMatch = username ? item.nickName.includes(username) : true;
      // console.log('nameMatch', nameMatch);
      // 如果 phone 存在且非空，才检查 item.phonenumber.includes(phone)
      const phoneMatch = phone ? item.phonenumber.includes(phone) : true;
      const statusMatch = status ? Number(status) === item.status : true;

      // 只有 nameMatch 和 phoneMatch 都为 true，才保留这个项
      return nameMatch && phoneMatch && statusMatch;
    });

    console.log("list", list, body);
  }
  return {
    code: "0",
    msg: "success",
    tableList: tableRoleList,
  };
});

//删除角色
Mock.mock("/api/delRoleIds", "post", function (option) {
  const { body } = option;
  // let list = tableUserList
  if (body) {
    const obj = JSON.parse(body);
    const ids = obj.ids;
    console.log("obj", ids);
    tableRoleList = tableRoleList.filter((item) => !ids.includes(item.key));
  }
  return {
    code: "0",
    msg: "success",
    tableList: tableRoleList,
  };
});
//修改角色状态
Mock.mock("/api/updateRoleStatus", "post", function (option) {
  const { body } = option;
  if (body) {
    const obj = JSON.parse(body);
    const { key, status } = obj;
    console.log("updateRoleStatus", obj);

    // 根据key找到对应角色并修改状态
    tableRoleList = tableRoleList.map((item) => {
      return item.key === key ? { ...item, status: Number(status) } : item;
    });
  }
  return {
    code: "0",
    msg: "success",
    tableList: tableRoleList,
  };
});

//增加角色
Mock.mock("/api/addRoleIds", "post", function (option) {
  const { body } = option;
  // let list = tableUserList
  if (body) {
    const obj = JSON.parse(body);
    console.log("obj", obj);
    const user = obj.user;
    user.status = Number(user.status);
    user.createTime = new Date().getTime();
    user.key = Math.floor(Math.random() * 1000);
    tableRoleList.unshift(obj.user);
  }
  return {
    code: "0",
    msg: "success",
    tableList: tableRoleList,
  };
});

// 菜单管理
import menuList from "./menu.json";
// 获取菜单列表数据
Mock.mock("/api/menuList", "post", function (option) {
  // console.log('option',option);
  const { body } = option;
  let list = menuList;
  if (body) {
    const obj = JSON.parse(body);
    const { username, phone, status } = obj;

    list = menuList.filter((item) => {
      // 如果 name 存在且非空，才检查 item.nickName.includes(name)
      const nameMatch = username ? item.nickName.includes(username) : true;
      // console.log('nameMatch', nameMatch);
      // 如果 phone 存在且非空，才检查 item.phonenumber.includes(phone)
      const phoneMatch = phone ? item.phonenumber.includes(phone) : true;
      const statusMatch = status ? Number(status) === item.status : true;

      // 只有 nameMatch 和 phoneMatch 都为 true，才保留这个项
      return nameMatch && phoneMatch && statusMatch;
    });

    console.log("list", list, body);
  }
  return {
    code: "0",
    msg: "success",
    tableList: list,
  };
});
